package br.unifor.cct.mia.mutation;

import br.unifor.cct.mia.evolutionary.Genotype;
import br.unifor.cct.mia.ga.Ga;
import br.unifor.cct.mia.util.Methods;

public class MutationExclusive implements Mutation {
	
	private void mutate(Ga Ga) {        	
		double lbound, ubound;
        double x;
        
        for (int i = 0; i < Ga.configurations.getPopsize(); i++) {
            for (int j = 0; j < Ga.configurations.getNvars(); j++) {
                x = Methods.randDoubleValue(0, 1);
                if (x*100 < Ga.configurations.getPmutation()*100) {
                    lbound = ((Genotype)Ga.population[i]).getLower(j);
                    ubound = ((Genotype)Ga.population[i]).getUpper(j);
                    
                    int gene2 = Methods.randIntValue((int)lbound,(int)ubound);
                    
                    int aux = (int)((Genotype)Ga.population[i]).getGene(j);
                    ((Genotype)Ga.population[i]).setGene(((Genotype)Ga.population[i]).getGene(gene2),j);
                    ((Genotype)Ga.population[i]).setGene(aux,gene2);
                }
            }
        }
	}
	
	public Object execute(Object value) {
		Object[] o = (Object[])value;
		Ga Ga = (Ga)o[0];
		
        mutate(Ga);        
		return Ga.population;
	}
}
